package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.Goods;
import com.wms.entity.Record;
import com.wms.entity.Request;
import com.wms.service.GoodsService;
import com.wms.service.RecordService;
import com.wms.service.RequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2023-02-07
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private GoodsService goodsService;





    @PostMapping("/listPageCCC")
    public  Result listPageCCC(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String storage = (String)param.get("storage");
        String goodstype = (String)param.get("goodstype");
        String roleId = (String)param.get("roleId");
        String userId = (String)param.get("userId");


        System.out.println("name==="+(String)param.get("name"));



        Page<Record> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        QueryWrapper<Record> queryWrapper = new QueryWrapper();
        queryWrapper.apply(" a.goods=b.id AND  b.storage= c.id AND b.goodstype = d.id").last("order by a.id asc");

        if(StringUtils.isNotBlank(name)&&!"null".equals(name)){


            queryWrapper.like("b.name",name);



        }
        if(StringUtils.isNotBlank(storage)&&!"null".equals(storage)){

            queryWrapper.eq("c.id",storage);
        }
        if(StringUtils.isNotBlank(goodstype)&&!"null".equals(goodstype)){

            queryWrapper.eq("d.id",goodstype);

        }

        if("1".equals(roleId)){
            queryWrapper.apply("a.userId="+userId);

        }




        IPage result = recordService.pageCCC(page,queryWrapper);


        return Result.suc(result.getRecords(), result.getTotal());
    }

    @PostMapping("/save")
    public Result save(@RequestBody Record record)
    {

        Goods goods = goodsService.getById(record.getGoods());
        Integer count1 = record.getCount();/*记录中变动物品的数量*/
        if("2".equals(record.getAction())){
            count1 = -count1;
            record.setCount(count1);

        }




        Integer count = goods.getCount();  /* 变动物品前的数量*/
        Integer sum = count  + count1;     /*变动物品后的数量*/
        goods.setCount(sum);
        goodsService.updateById(goods);



        return recordService.save(record)?Result.suc():Result.fail();

    }


}
